var currentPage = 1; // 当前页
var tolPage = 0;
var firstResult = 1;
var targetPage = 1;
var tolCount = 0;
$(document).ready(function() {
	selectByPage();
});

// 输入的页数
$('.submit').click(function() {
	targetPage = Number($('.number').val());
	pageByInput();
});

// 点击页码跳转
function pageByInput() {
	if (targetPage < currentPage) {// 获取的num比当前页小
		firstResult = Number(firstResult - (currentPage - targetPage)
				* maxResult);
	} else if (targetPage > currentPage) {
		if (targetPage >= tolPage) {
			targetPage = tolPage;
			firstResult = Number((targetPage - 1) * maxResult);
		} else {
			firstResult = Number(firstResult + (targetPage - currentPage)
					* maxResult);  
		} 
	} else {
		return;
	}
	selectByPage();
	currentPage = targetPage;
}

// 初始化页数
function intipage() {

	$('#tolPage').html("共" + tolPage + "页");
	

	// 页码展示html
	$('#pageDetail').val(Number(currentPage));
	// 绑定点击事件 -- 点击页码
	$('.pageNumber').unbind('click').click(function() {
		targetPage = $(this).attr('value');// 获取当前点击按钮的value
		var page = 1;
		if (targetPage < currentPage) {// 获取的num比当前页小
			page = currentPage - targetPage;
			firstResult = Number(firstResult - page * maxResult);
		} else if (targetPage > currentPage) {
			page = targetPage - currentPage;
			firstResult = Number(firstResult + page * maxResult);
		} else {
			return;
		}
		currentPage = targetPage;
		selectByPage();
	});
}

$("#beforPage").click(function(){
	
	if(Number(firstResult - maxResult) >= 1){
		firstResult =  firstResult - maxResult;
		selectByPage();
	}
	
});

$("#nextPage").click(function(){
	if(Number(firstResult + maxResult) <= tolCount){
		firstResult =  firstResult + maxResult;//  0  10 20 30  29
		selectByPage();
	}
	
});
